/*
 * @Date: 2021-04-20 10:26:47
 * @Description:
 * @LastEditors: hejiajun
 */
import axios from './axios';
import config from './config';

const HttpRequest = new axios();

HttpRequest.baseUrl = config.baseUrl;

// 封装请求
function $http(
  data,
  des,
  options = {
    method: 'POST',
  }
) {
  const url = options.url ? options.url : config.baseUrl;

  return new Promise((resolve, reject) => {
    const { serviceId } = data; // 服务ID

    if (Object.keys(options).length === 1) if (!serviceId) reject('data对象内请传入serviceId');
    if (!des) reject('请传入接口说明');
    des = encodeURI(des, 'UTF-8');

    let params = {
      url: `${url}?v=${serviceId}/${des}`,
      ...options,
    };

    if (data) params['data'] = data;

    resolve(HttpRequest.request(params, des));
  });
}

/**
 *  post请求--是否登录
 */

const getInterfaceIsLoginData = () => {
  return HttpRequest.request({
    url: config.userLoginUrl,
    method: 'post',
  });
};

/**
 * post请求--获取IP接口
 */
const getInterfaceIpData = () => {
  return HttpRequest.request({
    url: config.ipUrl,
    method: 'post',
  });
};

// post请求--埋点监控接口
const getInterfaceMonitorData = (data, params) => {
  return HttpRequest.request({
    url: config.monitorUrl + '?v=' + params,
    data,
    method: 'post',
  });
};

export { $http, getInterfaceIsLoginData, getInterfaceIpData, getInterfaceMonitorData };
